package com.zzb.projectfactory.framework.event;

import com.zzb.projectfactory.framework.event.entity.SysEventNotify;
import com.zzb.projectfactory.framework.event.service.SysEventNotifyService;
import com.zzb.projectfactory.framework.utils.SpringContextHolder;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * 事件发布中心
 * Created by zzb on 17-2-21.
 */
public class EventHome {
    private static Logger logger = LoggerFactory.getLogger(EventHome.class);
    private static SysEventNotifyService sysEventNotifyService;
    static {
        sysEventNotifyService =  SpringContextHolder.getBean("sysEventNotifyService");

    }

    /**发布事件*/
    public  static void publishEvent(BaseEvent event){
        String eventId = event.getEventId();
        List<SysEventNotify> sysEventNotifyList = sysEventNotifyService.findByEventIdOrderByExecuteOrderAsc(eventId);
        if(CollectionUtils.isNotEmpty(sysEventNotifyList)){
            for (SysEventNotify sysEventNotify : sysEventNotifyList) {
                String springId = sysEventNotify.getExecuteSpringId();
                AbstractProcessor processor = SpringContextHolder.getBean(springId);
                processor.execute(event);
            }
        }
    }
}
